package com.smartx.app.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.smartx.app.R;
import com.smartx.app.SatApp;
import com.smartx.app.model.TransactionModel;
import com.smartx.util.ResourceUtil;

import java.util.List;

public class TransactionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG="Transaction";

    private Context mContext;
    private List<TransactionModel> mDatas;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    public static final int PULLUP_LOAD_MORE = 0;
    public static final int LOADING_MORE = 1;

    private int load_more_status = 0;

    public TransactionListAdapter(Context mContext, List<TransactionModel> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    public void setData(List<TransactionModel> mDatas){
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG,"on create view holder");
        if (viewType == TYPE_ITEM) {
            Log.d(TAG,"on create item view holder");
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_transaction, parent,false);
            return new ItemHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_footer, parent,false);
            return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemHolder) {

            ItemHolder itemHolder = (ItemHolder) holder;
            TransactionModel bean = mDatas.get(position);
            if(bean.to.equals(SatApp.getInstance().getCoinBaseAddress())){
                itemHolder.ivImg.setImageResource(R.drawable.ic_arrow_downward_black_24dp);
                itemHolder.txAddr.setText(bean.from);
                itemHolder.txType.setText("receive");
                itemHolder.txType.setTextColor(ResourceUtil.getColor(R.color.green));
                itemHolder.txValue.setText("+"+bean.value);
                itemHolder.txValue.setTextColor(ResourceUtil.getColor(R.color.green));

            }else {
                itemHolder.ivImg.setImageResource(R.drawable.ic_arrow_upward_black_24dp);
                itemHolder.txAddr.setText(bean.to);
                itemHolder.txType.setText("sent");
                itemHolder.txType.setTextColor(ResourceUtil.getColor(R.color.red));
                itemHolder.txValue.setText("-"+bean.value);
                itemHolder.txValue.setTextColor(ResourceUtil.getColor(R.color.red));
            }
        }
        else if (holder instanceof FooterHolder) {
            FooterHolder footHolder = (FooterHolder) holder;
            footHolder.linearLayout.setVisibility(View.GONE);
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footHolder.linearLayout.setVisibility(View.GONE);
                    footHolder.textView.setText("to the bottom");
                    break;
                case LOADING_MORE:
                    footHolder.linearLayout.setVisibility(View.VISIBLE);
                    footHolder.textView.setText("loading more ...");
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size() + 1;
    }


    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        private ImageView ivImg;
        private TextView txAddr;
        private TextView txType;
        private TextView txValue;
        public ItemHolder(View itemView) {
            super(itemView);
            ivImg = (ImageView) itemView.findViewById(R.id.type_icon);
            txType = (TextView) itemView.findViewById(R.id.type);
            txAddr = (TextView) itemView.findViewById(R.id.address);
            txValue = (TextView) itemView.findViewById(R.id.value);
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public LinearLayout linearLayout;

        public FooterHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_item_recycler_footer);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_recycler_footer);
        }
    }

    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }
}